import 'package:core_package/ui/alert/show_bottom_alert_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/model/channel_model.dart';
import 'package:flutter_app_unionad/request/workbench_request.dart';
import 'package:core_package/core_package.dart';

import 'package:wifi/wifi.dart';

class SelectWifWidget extends StatefulWidget {
  @override
  _SelectWifWidgetState createState() => _SelectWifWidgetState();

  static Future show(BuildContext context) async {
    return ShowBottomAlertDialog.showBottomDialog(context, builder: (ctx) {
      return SelectWifWidget();
    });
  }
}

class _SelectWifWidgetState extends State<SelectWifWidget> {
  List<WifiResult>? list;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Wifi.list('').then((value) {
      setState(() {
        list = value;
      });
    }); // this key is used to filter
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 15.px),
      child: Column(
        children: [
          MySizeBox(height: 10),
          ShowTitleDetailListWidget(
            title: '选择Wifi',
            lineColor: MyColors.color_151_151_151,
            padding: EdgeInsets.zero,
            linePadding: EdgeInsets.zero,
            icon: IconButton(
              onPressed: () => Navigator.pop(context),
              icon: Icon(
                Icons.close,
                size: 24,
                color: MyColors.color_34_34_34,
              ),
            ),
          ),
          Expanded(child: buildContent()),
        ],
      ),
    );
  }

  Widget buildContent() {
    if (list == null) return ProgressDialog.showFirstLoading();
    if (list!.length == 0)
      return EmptyWidget(
        msg: '未找到可用wifi',
      );
    return ListView.builder(
      itemCount: list!.length,
      itemBuilder: (context, index) {
        return ShowTitleDetailListWidget(
          showBottomLine: false,
          padding: EdgeInsets.symmetric(horizontal: 5.px, vertical: 5.px),
          linePadding: EdgeInsets.symmetric(horizontal: 5.px, vertical: 5.px),
          title: list![index].ssid,
          onTap: () => Navigator.pop(context, list![index].ssid),
        );
      },
      padding: EdgeInsets.zero,
    );
  }
}
